package cn.iocoder.yudao.module.pay.wallet.service;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.business.basic.dao.repository.SerialNoRedisRepository;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.pay.wallet.enums.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.wallet.bo.WalletTransactionCreateReqBO;
import cn.iocoder.yudao.module.pay.wallet.convert.PayWalletTransactionConvert;
import cn.iocoder.yudao.module.pay.wallet.dal.dataobject.PayWalletDO;
import cn.iocoder.yudao.module.pay.wallet.dal.dataobject.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.wallet.dal.mysql.PayWalletTransactionMapper;
import cn.iocoder.yudao.module.pay.wallet.vo.AppPayWalletTransactionPageReqVO;
import cn.iocoder.yudao.module.pay.wallet.vo.AppPayWalletTransactionSummaryRespVO;
import cn.iocoder.yudao.module.pay.wallet.vo.PayWalletTransactionPageReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;

import static cn.iocoder.yudao.module.api.pay.ConfigConstants.PAY_WALLET_NO_PREFIX;

/**
 * 钱包流水 Service 实现类
 *
 * @author jason
 */
@Service
@Slf4j
@Validated
public class PayWalletTransactionServiceImpl implements PayWalletTransactionService {

    @Resource
    private PayWalletService payWalletService;
    @Resource
    private PayWalletTransactionMapper payWalletTransactionMapper;
    @Resource
    private SerialNoRedisRepository serialNoRedisRepository;

    @Override
    public PageResult<PayWalletTransactionDO> getWalletTransactionPage(Long userId, Integer userType,
                                                                       AppPayWalletTransactionPageReqVO pageVO) {
        PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
        return payWalletTransactionMapper.selectPage(wallet.getId(), pageVO.getType(), pageVO, pageVO.getCreateTime());
    }

    @Override
    public PageResult<PayWalletTransactionDO> getWalletTransactionPage(PayWalletTransactionPageReqVO pageVO) {
        // 基于 userId + userType 查询钱包
        if (pageVO.getWalletId() == null
            && ObjectUtil.isAllNotEmpty(pageVO.getUserId(), pageVO.getUserType())) {
            PayWalletDO wallet = payWalletService.getOrCreateWallet(pageVO.getUserId(), pageVO.getUserType());
            if (wallet != null) {
                pageVO.setWalletId(wallet.getId());
            }
        }

        // 查询分页
        return payWalletTransactionMapper.selectPage(pageVO.getWalletId(), null, pageVO, null);
    }

    @Override
    public PayWalletTransactionDO createWalletTransaction(WalletTransactionCreateReqBO bo) {
        PayWalletTransactionDO transactionDO = PayWalletTransactionConvert.INSTANCE.convert(bo);
        transactionDO.setNo(serialNoRedisRepository.generateNo(PAY_WALLET_NO_PREFIX));
        payWalletTransactionMapper.insert(transactionDO);
        return transactionDO;
    }

    @Override
    public PayWalletTransactionDO getWalletTransactionByNo(String no) {
        return payWalletTransactionMapper.selectByNo(no);
    }

    @Override
    public PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type) {
        return payWalletTransactionMapper.selectByBiz(bizId, type.getValue());
    }

    @Override
    public AppPayWalletTransactionSummaryRespVO getWalletTransactionSummary(Long userId, Integer userType, LocalDateTime[] createTime) {
        PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
        AppPayWalletTransactionSummaryRespVO respVO = new AppPayWalletTransactionSummaryRespVO();
        respVO.setTotalExpense(payWalletTransactionMapper.selectPriceSum(wallet.getId(), false, createTime));
        respVO.setTotalIncome(payWalletTransactionMapper.selectPriceSum(wallet.getId(), true, createTime));
        return respVO;
    }

}
